home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr13 / tstse13.zip / COUNT.S next >
Text File  |  1995-02-12  |  7KB  |  267 lines

  1. /* Count lines, words and characters in a marked block for SemWare's
  2.    TSE editor V2.0. To make this SAL macro operational, invoke the
  3.    main menu (F10), choose "Macro", choose "Compile" and press Enter
  4.    at "Execute Macro".
  5.  
  6. ..................................................................
  7. Prof. Timo Salmi      Co-moderator of comp.archives.msdos.announce
  8. Moderating at garbo.uwasa.fi anonymous FTP archives  193.166.120.5
  9. Department of Accounting and Business Finance; University of Vaasa
  10. Internet: ts@uwasa.fi   BBS +(358)-61-3170972; FIN-65101,  Finland
  11. */
  12.  
  13. // The contents of a simple help, tied later to the CtrlAlt-H key
  14. helpdef tHelpData
  15.   title = "COUNT.S HELP"           // The help's caption
  16.   x = 10                           // Location
  17.   y = 3
  18.   // The actual help text
  19.   " Prof. Timo Salmi's Unix wc like count."
  20.   ""
  21.   " This macro calculates the number of lines, words "
  22.   " and characters in a marked block."
  23.   ""
  24.   " Leading but not the trailing blanks are included "
  25.   " in the character count. The counts are not "
  26.   " always fully accurate. The results may depend on "
  27.   " the relative location of the block in the file. "
  28.   ""
  29.   " You can use <F11> to invoke the command menu "
  30.   " after first exiting this help. "
  31.   ""
  32.   " Last updated Sun 12-February-1995 08:25:22 "
  33. end  /* tHelpData */
  34.  
  35. // Count the number of lines in a block
  36. //
  37. integer proc tCountLinesInBlock()
  38.   integer lines = 0
  39.   integer key
  40.   PushPosition()
  41.   GoToblockBegin()
  42.   repeat
  43.     if KeyPressed()
  44.       key = GetKey()
  45.       if key == <escape>
  46.         return(-1)
  47.       endif
  48.     endif
  49.     if not RollDown() break endif
  50.     if not isCursorInBlock() break endif
  51.     lines = lines + 1
  52.     if lines mod 10 == 0
  53.       Message ("Counting lines, <Esc> aborts: " + Str(lines))
  54.     endif
  55.   until FALSE
  56.   PopPosition()
  57.   if isBlockMarked() == _LINE_ return(lines+1) endif
  58.   return(lines)
  59. end tCountLinesInBlock
  60.  
  61. // Count the number of words in a block
  62. //
  63. integer proc tCountWordsInBlock()
  64.   integer words = 0
  65.   integer flag = FALSE
  66.   integer key
  67.   PushPosition()
  68.   GoToblockBegin()
  69.   repeat
  70.     if not isCursorInBlock() break endif
  71.     if not NextChar() break endif
  72.   until CurrChar() <> 32 and CurrChar() <> 9 and CurrChar() <> 12
  73.   flag = TRUE
  74.   repeat
  75.     if KeyPressed()
  76.       key = GetKey()
  77.       if key == <escape>
  78.         return(-1)
  79.       endif
  80.     endif
  81.     if CurrChar() == 32 or CurrChar() ==  9 or
  82.        CurrChar() == 12 or CurrChar() < 0
  83.       if flag words = words + 1 endif
  84.       if words mod 100 == 0
  85.         Message ("Counting words, <Esc> aborts: "+ Str(words))
  86.       endif
  87.       flag = FALSE
  88.     else
  89.       flag = TRUE
  90.     endif
  91.   until not isCursorInBlock() or not NextChar()
  92.   PopPosition()
  93.   return(words)
  94. end tCountWordsInBlock
  95.  
  96. // Count the number of characters in a block
  97. //
  98. integer proc tCountCharsInBlock()
  99.   integer chars = 0
  100.   integer key
  101.   PushPosition()
  102.   GoToblockBegin()
  103.   repeat
  104.     if KeyPressed()
  105.       key = GetKey()
  106.       if key == <escape>
  107.         return(-1)
  108.       endif
  109.     endif
  110.     if CurrChar() == _BEYOND_EOL_
  111.       chars = chars - 1
  112.     endif
  113.     if not isCursorInBlock() break endif
  114.     if not NextChar() break endif
  115.     if CurrChar() >= 0
  116.       chars = chars + 1
  117.     endif
  118.     if chars mod 1000 == 0
  119.       Message ("Counting characters, <Esc> aborts: " + Str(chars))
  120.     endif
  121.   until FALSE
  122.   PopPosition()
  123.   return(chars)
  124. end tCountCharsInBlock
  125.  
  126. // Call only counting lines
  127. //
  128. proc tCallCountLines()
  129.   integer lines
  130.   string s[79] = ""
  131.   if isBlockInCurrFile() == _COLUMN_
  132.     Warn ("Column blocks not allowed for COUNT")
  133.   elseif isBlockInCurrFile()
  134.     lines  = tCountLinesInBlock()
  135.     if lines == -1
  136.       s = "Aborted"
  137.     else
  138.       s = Str(lines) + " lines "
  139.     endif
  140.     if lines <> -1 s = s + "in the marked block" endif
  141.     Message(s)
  142.   else
  143.     Warn ("No marked block in the current window")
  144.   endif
  145. end tCallCountLines
  146.  
  147. // Call only counting words
  148. //
  149. proc tCallCountWords()
  150.   integer words
  151.   string s[79] = ""
  152.   if isBlockInCurrFile() == _COLUMN_
  153.     Warn ("Column blocks not allowed for COUNT")
  154.   elseif isBlockInCurrFile()
  155.     words  = tCountWordsInBlock()
  156.     if words == -1
  157.       s = "Aborted"
  158.     else
  159.       s = Str(words) + " words "
  160.     endif
  161.     if words <> -1 s = s + "in the marked block" endif
  162.     Message(s)
  163.   else
  164.     Warn ("No marked block in the current window")
  165.   endif
  166. end tCallCountWords
  167.  
  168. // Call only counting characters
  169. //
  170. proc tCallCountChars()
  171.   integer chars
  172.   string s[79] = ""
  173.   if isBlockInCurrFile() == _COLUMN_
  174.     Warn ("Column blocks not allowed for COUNT")
  175.   elseif isBlockInCurrFile()
  176.     chars  = tCountCharsInBlock()
  177.     if chars == -1
  178.       s = "Aborted"
  179.     else
  180.       s = Str(chars) + " characters "
  181.     endif
  182.     if chars <> -1 s = s + "in the marked block" endif
  183.     Message(s)
  184.   else
  185.     Warn ("No marked block in the current window")
  186.   endif
  187. end tCallCountChars
  188.  
  189. // Call counting lines, words and characters
  190. //
  191. proc tCallCountAll()
  192.   integer lines, words, chars
  193.   string s[79] = ""
  194.   if isBlockInCurrFile() == _COLUMN_
  195.     Warn ("Column blocks not allowed for COUNT")
  196.   elseif isBlockInCurrFile()
  197.     lines  = tCountLinesInBlock()
  198.     if lines == -1
  199.       s = "Aborted"
  200.       goto _show
  201.     else
  202.       s = Str(lines) + " lines "
  203.     endif
  204.     //
  205.     words = tCountWordsInBlock()
  206.     if words == -1
  207.       goto _show
  208.     else
  209.       s = s + Str(words) + " words "
  210.     endif
  211.     //
  212.     chars = tCountCharsInBlock()
  213.     if chars == -1
  214.       goto _show
  215.     else
  216.       s = s + Str(chars) + " characters "
  217.     endif
  218.     _show:
  219.     if lines <> -1 s = s + "in the marked block" endif
  220.     Message(s)
  221.   else
  222.     Warn ("No marked block in the current window")
  223.   endif
  224. end tCallCountAll
  225.  
  226. // New keys and menus **************************************************
  227. forward Menu tCountMenu()
  228. forward proc tDisableNewKeys()
  229.  
  230. // Add the new key definitions
  231. keydef new_keys
  232.   <CtrlAlt 2>      tCallCountLines()
  233.   <CtrlAlt 3>      tCallCountWords()
  234.   <CtrlAlt 4>      tCallCountChars()
  235.   <CtrlAlt 5>      tCallCountAll()
  236.   <CtrlAlt 0>      tDisableNewKeys()
  237.   <CtrlAlt H>      QuickHelp(tHelpData)
  238.   <F11>            tCountMenu()
  239. end
  240.  
  241. // Disabling the new extra keys ***************************************
  242. proc tDisableNewKeys()
  243.   if YesNo("Disable the extra keys:") == 1 Disable(new_keys) endif
  244. end
  245.  
  246. // The count menu *****************************************************
  247. Menu tCountMenu()
  248.   Title = "Timo's count menu"
  249.   Width = 19
  250.   x = 40
  251.   y = 3
  252.   history
  253.   "Count &lines       <CtrlAlt 2>"   , tCallCountLines()
  254.   "Count &words       <CtrlAlt 3>"   , tCallCountLines()
  255.   "Count &characters  <CtrlAlt 4>"   , tCallCountChars()
  256.   "Count &all         <CtrlAlt 5>"   , tCallCountAll()
  257.   "",,Divide
  258.   "Disable &new keys  <CtrlAlt 0>"   , tDisableNewKeys()
  259.   "&Help              <CtrlAlt H>"   , QuickHelp(tHelpData)
  260.   "This Menu         <F11>"
  261. end tCountMenu
  262.  
  263. proc Main()
  264.   Enable (new_keys)
  265.   tCountMenu()
  266. end
  267.